package de.piratech.dasding.data.database.mongo.data;

import java.util.List;

import net.vz.mongodb.jackson.DBRef;
import de.piratech.dasding.data.Action;
import de.piratech.dasding.data.Action.ACTIONSTATUS;
import de.piratech.dasding.data.Event;
import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.mongo.AbstractMongoDB.COLLECTIONS;

public class MongoAction extends AbstractMongoAction<Action> {

  private DBRef<MongoEvent, String> eventRef;
  private List<DBRef<MongoUser, String>> participantRefs;
  private DBRef<MongoActionCategory, String> categoryRef;

  public MongoAction() {
    super(new Action());
  }

  public MongoAction(Action object) {
    super(object);
    if (object.getParticipants() != null) {
      for (User participant : object.getParticipants()) {
        participantRefs.add(new DBRef<MongoUser, String>(participant.getId(),
            COLLECTIONS.USER_COLLECTION.getName()));
      }
    }
    if (object.getEvent() != null) {
      eventRef = new DBRef<MongoEvent, String>(object.getEvent().getId(),
          COLLECTIONS.EVENT_COLLECTION.getName());
    }
    if (object.getCategory() != null) {
      categoryRef = new DBRef<MongoActionCategory, String>(object.getCategory()
          .getId(), COLLECTIONS.ACTIONCATEGORY_COLLECTION.getName());
    }
  }

  public ACTIONSTATUS getStatus() {
    return getDataObject().getStatus();
  }

  public void setStatus(ACTIONSTATUS pStatus) {
    getDataObject().setStatus(pStatus);
  }

  public List<DBRef<MongoUser, String>> getParticipantsRefs() {
    return participantRefs;
  }

  public void setParticipantsRefs(List<DBRef<MongoUser, String>> pParticipants) {
    for (DBRef<MongoUser, String> user : pParticipants) {
      getDataObject().addParticipant(user.fetch().getDataObject());
    }
  }

  public DBRef<MongoActionCategory, String> getCategoryRef() {
    return categoryRef;
  }

  public void setCategoryRef(DBRef<MongoActionCategory, String> pCategory) {
    categoryRef = pCategory;
    getDataObject().setCategory(pCategory.fetch().getDataObject());
  }

  public DBRef<MongoEvent, String> getEventRef() {
    return eventRef;
  }

  public void setEventRef(DBRef<MongoEvent, String> eventRef) {
    this.eventRef = eventRef;
    // only set id here because event also references action
    Event event = new Event();
    event.setId(eventRef.getId());
    getDataObject().setEvent(event);
  }
}
